package com.bw.zcb.mvp.view.user;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bw.zcb.R;
import com.bw.zcb.mvp.base.model.BaseEntity;
import com.bw.zcb.mvp.base.view.BaseActivity;
import com.bw.zcb.mvp.contract.LoginContract;
import com.bw.zcb.mvp.di.DaggerLoginComponent;
import com.bw.zcb.mvp.di.LoginModules;
import com.bw.zcb.mvp.model.HttpCode;
import com.bw.zcb.mvp.model.LoginModel;
import com.bw.zcb.mvp.model.entity.ResponseUserEntity;
import com.bw.zcb.mvp.model.entity.UserEntity;
import com.bw.zcb.mvp.presenter.LoginPresenter;
import com.bw.zcb.network.tool.App;
import com.bw.zcb.utils.ToastUtils;

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.ILoginView,View.OnClickListener{


    private RelativeLayout phoneMode;
    private EditText editPhone;
    private EditText editCode;
    private Button loginCode;
    private Button phoneLogin;
    private TextView phoneModeCheckOne;
    private RelativeLayout codeMode;
    private EditText phoneName;
    private EditText phonePwd;
    private Button codeLogin;
    private TextView phoneModeCheckTwo;
    private LoginPresenter loginPresenter;
    private RelativeLayout login;
    private android.widget.ImageView pwdVisibility;
    private boolean flag = true;
    private Button reg;

    @Override
    public View addStatusView() {
        return f(R.id.login);
    }
    public int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        phoneMode = (RelativeLayout) findViewById(R.id.phone_mode);
        editPhone = (EditText) findViewById(R.id.edit_phone);
        editCode = (EditText) findViewById(R.id.edit_code);
        loginCode = (Button) findViewById(R.id.login_code);
        phoneLogin = (Button) findViewById(R.id.phone_login);
        phoneModeCheckOne = (TextView) findViewById(R.id.phone_mode_check_one);
        codeMode = (RelativeLayout) findViewById(R.id.code_mode);
        phoneName = (EditText) findViewById(R.id.phone_name);
        phonePwd = (EditText) findViewById(R.id.phone_pwd);
        codeLogin = (Button) findViewById(R.id.code_login);
        phoneModeCheckTwo = (TextView) findViewById(R.id.phone_mode_check_two);
        login = (RelativeLayout) findViewById(R.id.login);
        pwdVisibility = (ImageView) findViewById(R.id.pwd_visibility);
        codeLogin.setOnClickListener(this::onClick);
        phoneLogin.setOnClickListener(this::onClick);
        pwdVisibility.setOnClickListener(this::onClick);
        reg = (Button) findViewById(R.id.reg);
        reg.setOnClickListener(this::onClick);
        phoneModeCheckOne();
        phoneModeCheckTwo();
        StatusView(Color.WHITE);

    }
    private void phoneModeCheckTwo() {
        phoneModeCheckTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimatorSet animatorSet = new AnimatorSet();
                ObjectAnimator translationY = ObjectAnimator.ofFloat(codeMode, "translationY", 0, 200);
                ObjectAnimator alpha = ObjectAnimator.ofFloat(codeMode, "alpha", 1, 0);
                animatorSet.setInterpolator(new LinearInterpolator());
                animatorSet.setDuration(1000);
                animatorSet.playTogether(translationY,alpha);
                animatorSet.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        codeMode.setVisibility(View.GONE);
                        phoneMode.setVisibility(View.VISIBLE);
                        editPhone.setText("");
                        editCode.setText("");
                        AnimatorSet animatorSet1 = new AnimatorSet();
                        ObjectAnimator alpha1 = ObjectAnimator.ofFloat(phoneMode, "alpha", 0, 1);
                        ObjectAnimator translationY1 = ObjectAnimator.ofFloat(phoneMode, "translationY", 200, 0);
                        animatorSet1.setInterpolator(new LinearInterpolator());
                        animatorSet1.setDuration(1000);
                        animatorSet1.playTogether(translationY1,alpha1);
                        animatorSet1.start();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }
                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                animatorSet.start();
            }
        });
    }
    private void phoneModeCheckOne() {
        phoneModeCheckOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimatorSet animatorSet = new AnimatorSet();
                ObjectAnimator translationY = ObjectAnimator.ofFloat(phoneMode, "translationY", 0, 200);
                ObjectAnimator alpha = ObjectAnimator.ofFloat(phoneMode, "alpha", 1, 0);
                animatorSet.setInterpolator(new LinearInterpolator());
                animatorSet.setDuration(1000);
                animatorSet.playTogether(translationY,alpha);
                animatorSet.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        codeMode.setVisibility(View.VISIBLE);
                        phoneMode.setVisibility(View.GONE);
                        editPhone.setText("");
                        editCode.setText("");
                        AnimatorSet animatorSet1 = new AnimatorSet();
                        ObjectAnimator alpha1 = ObjectAnimator.ofFloat(codeMode, "alpha", 0, 1);
                        ObjectAnimator translationY1 = ObjectAnimator.ofFloat(codeMode, "translationY", 200, 0);
                        animatorSet1.setInterpolator(new LinearInterpolator());
                        animatorSet1.setDuration(1000);
                        animatorSet1.playTogether(translationY1,alpha1);
                        animatorSet1.start();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }
                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                animatorSet.start();
            }
        });

    }
    @Override
    public void initData() {
        loginPresenter = new LoginPresenter(new LoginModel(),this);
    }

    @Override
    public void inject() {
        DaggerLoginComponent.builder().loginModules(new LoginModules(this)).build().injectLogin(this);
    }

    @Override
    public void refreshView(BaseEntity baseEntity) {
        ResponseUserEntity entity = (ResponseUserEntity) baseEntity;
        if (entity.status.equals("200")){
            UserEntity values = entity.getValues();
            int user_id = values.getUser_id();
            String user_img = values.getUser_img();
            int user_integral = values.getUser_integral();
            String user_name = values.getUser_name();
            int user_phone = values.getUser_phone();
            String user_pwd = values.getUser_pwd();
            String user_sex = values.getUser_sex();
            App.getInstance().getDaoSesson().insert(new UserEntity(user_id,user_phone,user_pwd,user_name,user_sex,user_img,user_integral));
            startPage(PersonActivity.class);
            finish();
        }

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.code_login==id){
            if (phoneName.getText().toString().isEmpty()){
                ToastUtils.showMsg("手机号为空");
                return;
            }
            if (phonePwd.getText().toString().isEmpty()){
                ToastUtils.showMsg("密码为空");
                return;
            }

            p.requestLogin(Long.valueOf(phoneName.getText().toString()),phonePwd.getText().toString(), HttpCode.LOGINCODE);
            ToastUtils.showMsg("登录成功");
        }
        if (R.id.pwd_visibility==id){
            if (flag){
               flag=false;
                pwdVisibility.setImageResource(R.drawable.res_7icon_202);
                phonePwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//隐藏密码
            }else{
                flag=true;
                pwdVisibility.setImageResource(R.drawable.res_7icon_201);
                phonePwd.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示密码
            }
            phonePwd.setSelection(phonePwd.getText().toString().length());
        }
        if (R.id.reg==id){
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        }
    }
}
